#include "fig_playerbullet_master.h"
#include "fig_bullet_manager.h"
#include "fig_enemy_manager.h"
#include "fig_player_craft.h"
#include "fighter.h"
#include "fig_sounds.h"

FIGPlayerBulletMaster::FIGPlayerBulletMaster() : m_NormalDir(math::Vec2(0,-1.0f))
{
	//Type, int GroupCount, float BulletFrequency, float DelayAfterBulletGroup, bool IsPlayer);
	FIGBulletManager*	pNormalBulletManager = 	new FIGBulletManager(
																	FIGBullet::BULLET_TYPE_PLAYER_BLUE,
																	5,
																	0.11f,
																	0.20f,
																	true
																	);
												 
	m_BulletManagers[BULLET_TYPES_BLUE] = pNormalBulletManager;

	//--------------------------------------------------------------
	pNormalBulletManager = new FIGBulletManager(
																	FIGBullet::BULLET_TYPE_PLAYER_RED,
																	3,
																	0.11f,
																	0.25f,
																	true
																	);
	m_BulletManagers[BULLET_TYPES_RED] = pNormalBulletManager;


	//-------------------------------------------------------
	pNormalBulletManager = new FIGBulletManager(
																	FIGBullet::BULLET_TYPE_PLAYER_GREEN,
																	3,
																	0.11f,
																	0.25f,
																	true
																	);
	m_BulletManagers[BULLET_TYPES_GREEN] = pNormalBulletManager;

	//-----------------------------------------------------------
	FIGBulletManager*	pRocketBulletManager = new FIGBulletManager(
																	FIGBullet::BULLET_TYPE_PLAYER_ROCKET,
																	1,
																	0.11f,
																	1.2f,		// DealyAfter Bullet Group
																	true
																	);
	m_BulletManagers[BULLET_TYPES_ROCKET] = pRocketBulletManager;

	//----------------------------------------------------------
	FIGBulletManager*	pTopidoBulletManager = new FIGBulletManager(
																	FIGBullet::BULLET_TYPE_PLAYER_TOPIDO,
																	1,
																	0.11f,
																	1.5f,
																	true
																	);
	m_BulletManagers[BULLET_TYPES_TOPIDO] = pTopidoBulletManager;


	
	
	
}

FIGPlayerBulletMaster&		FIGPlayerBulletMaster::Instance()
{
	static FIGPlayerBulletMaster Inst;
	return Inst;
}


void	FIGPlayerBulletMaster::Render(const math::Vec2& Offset,float Scale)
{
	for(int i = 0; i < (int)BULLET_TYPES_COUNT; i++)
	{
		FIGBulletManager* pBulletMananger = m_BulletManagers[i];
		pBulletMananger->Render(Offset,Scale);
	}
}

void	FIGPlayerBulletMaster::RenderDebug(const math::Vec2& Offset,float Scale)
{
	for(int i = 0; i < (int)BULLET_TYPES_COUNT; i++)
	{
		FIGBulletManager* pBulletMananger = m_BulletManagers[i];
		pBulletMananger->RenderDebug(Offset,Scale);
	}
}

void	FIGPlayerBulletMaster::Tick(float dt)
{
	for(int i = 0; i < (int)BULLET_TYPES_COUNT; i++)
	{
		FIGBulletManager* pBulletMananger = m_BulletManagers[i];
		pBulletMananger->Tick(dt);

		for(int i = 0 ; i < pBulletMananger->GetCount(); i++)
		{
			FIGBullet* pBullet = pBulletMananger->GetBulletByIndex(i);
			if(pBullet->IsAlive())
				FIGEnemyManager::Instance().HitTest(pBullet);

			if(pBullet->IsAlive())
			{
				FIGLevel* pLevel = Fighter::Instance().GetLevel();
				if(pLevel)
				{
					pLevel->HitTest(pBullet);
				}
			}
			
		}

	}

	
}

void	FIGPlayerBulletMaster::ShootTick(
										 const math::Vec2& Pos,
										 FIGPlayerBulletMaster::BULLET_TYPES Type, 
										 float dt,
										 FIGBullet::SHOOT_STYLES ShootStyle
										 )
{
	FIGSounds::SOUND_NAMES SoundSfx = FIGSounds::BULLETSOUND_NORMAL;

	if( Type == BULLET_TYPES_ROCKET ||
		Type == BULLET_TYPES_TOPIDO)
		SoundSfx = FIGSounds::ROCKET_TRAVEL;
			

	FIGBulletManager* pBulletMananger = NULL;
	if((int)Type < (int)BULLET_TYPES_COUNT)
		pBulletMananger = m_BulletManagers[Type];
	
	if(pBulletMananger)
		pBulletMananger->ShootTick(
									&FIGPlayerCraft::Instance(),
									Pos,
									dt , 
									m_NormalDir,
									ShootStyle,
									SoundSfx
									);
	
}


void	FIGPlayerBulletMaster::ShootRocketAllDir(const math::Vec2& Pos)
{
	FIGBulletManager* pBulletMananger = NULL;
	pBulletMananger = m_BulletManagers[FIGBullet::BULLET_TYPE_PLAYER_ROCKET];

	if(pBulletMananger)
	{
		pBulletMananger->ShootToAllDir(Pos);
	}

}

void	FIGPlayerBulletMaster::Clear()
{
	for( int i = (int)BULLET_TYPES_COUNT - 1; i >= 0; i--)
	{
		FIGBulletManager*  pObj = m_BulletManagers[i];
		pObj->Clear();
		//UTIL_SAFE_DELETE(pObj);
	}
	// manager shouldn't got deleted
	//m_BulletManagers.resize(0);

	
}

FIGPlayerBulletMaster::~FIGPlayerBulletMaster()
{
	for( int i = (int)BULLET_TYPES_COUNT - 1; i >= 0; i--)
	{
		FIGBulletManager*  pObj = m_BulletManagers[i];
		UTIL_SAFE_DELETE(pObj);
	}
}


	
